package com.senseauto.openglbitmap;

import android.graphics.Bitmap;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

public class MyRender implements GLSurfaceView.Renderer {
    private MyGLSurfaceView glSurfaceView;
    Bitmap bitmap;
    BitmapSquare bitmapSquare;
    int glTextureId;

    public void setBitmap(Bitmap bitmap) {
        this.bitmap = bitmap;
    }

    public MyRender(MyGLSurfaceView surfaceView) {
        this.glSurfaceView = surfaceView;
        bitmapSquare = new BitmapSquare();

    }


    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        bitmapSquare = new BitmapSquare();
        // 创建纹理
        glTextureId = OpenGLUtils.createTexture(bitmap, GLES20.GL_NEAREST, GLES20.GL_LINEAR,
                GLES20.GL_CLAMP_TO_EDGE, GLES20.GL_CLAMP_TO_EDGE);
    }

    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        GLES20.glViewport(0, 0, width, height);
    }

    @Override
    public void onDrawFrame(GL10 gl) {
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
        bitmapSquare.draw(glTextureId);
    }
}
